package qjc.n_贪心算法;

public class n1_买卖股票的最佳时机 {
    /**
     * 最低价买入，最高价卖出
     *
     * @param prices
     * @return
     */
    public int maxProfit(int[] prices) {
        if (prices == null || prices.length == 0) {
            return 0;
        }
        int minNum = prices[0];
        int maxNum = 0;
        for (int i = 1; i < prices.length; i++) {
            if (prices[i] < minNum) {
                minNum = prices[i];
            } else {
                maxNum = Math.max(maxNum, prices[i] - minNum);
            }
        }
        return maxNum;
    }
}
